//
//  ViewController.m
//  打飞机
//
//  Created by wangwei on 16/9/21.
//  Copyright © 2016年 itcast. All rights reserved.
//

#import "PlaneController.h"
#import "planeButton.h"
#define SCREENSIZE [UIScreen mainScreen].bounds.size

@interface PlaneController ()
@property (nonatomic,weak) UIImageView *bk1;
@property (nonatomic,weak) UIImageView *bk2;

@property(nonatomic,strong)NSMutableArray <UIImageView *>*bulletArray;
@end

@implementation PlaneController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    // Do any additional setup after loading the view, typically from a nib.
    [self shotBoom];
    
    planeButton *planeBtn = [[planeButton alloc]init];
    [self.view addSubview:planeBtn];
    
    [planeBtn setImage:[UIImage imageNamed:@"hero1"] forState:UIControlStateNormal];
    [planeBtn setImage:[UIImage imageNamed:@"hero2"] forState:UIControlStateHighlighted];
    
    planeBtn.bounds = CGRectMake(0, 0, 50, 60);
    planeBtn.center = self.view.center;
    
    [self animationPlay];
}


-(void)setupUI{
    
    UIImageView *backGroundImage1 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"background"]];
    _bk1 = backGroundImage1;
    UIImageView *backGroundImage2 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"background"]];
    _bk2 = backGroundImage2;
    [self.view addSubview:backGroundImage1];
    [self.view addSubview:backGroundImage2];
    [backGroundImage1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    [backGroundImage2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.view);
        make.bottom.equalTo(backGroundImage1.mas_top);
        make.height.mas_equalTo(self.view);
    }];
    
   
    
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:backBtn];
    [backBtn setTitle:@"返回主菜单" forState:UIControlStateNormal];
    [backBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    backBtn.titleLabel.font = MYFONT(25);
    [backBtn sizeToFit];
    
    [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(self.view).offset(-20);
        make.top.equalTo(self.view).offset(20);
    }];
    
    [backBtn addTarget:self action:@selector(clickBackBtn) forControlEvents:UIControlEventTouchUpInside];
}


-(void)animationPlay{
//    [UIView animateWithDuration:4 animations:^{
//        _bk1.transform = CGAffineTransformMakeTranslation(0, SCREEN_HEIGHT);
//    }completion:^(BOOL finished) {
//        _bk1.transform = CGAffineTransformIdentity;
//        [self animationPlay];
//    }];
    
    [UIView animateWithDuration:4 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
        _bk1.transform = CGAffineTransformMakeTranslation(0, SCREEN_HEIGHT);
        _bk2.transform = CGAffineTransformMakeTranslation(0, SCREEN_HEIGHT);
    } completion:^(BOOL finished) {
        _bk1.transform = CGAffineTransformIdentity;
        _bk2.transform = CGAffineTransformIdentity;
        [self animationPlay];
    }];
}


-(void)clickBackBtn{
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



-(void)shotBoom{
   
    
    
    
   
    [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(shootFunction) userInfo:nil repeats:YES];
    
    
}

-(void)shootFunction{
    
    //发送初始位置
    CGFloat startLocX = arc4random_uniform(SCREENSIZE.width * 10)/10.0;
    CGPoint startLoc = CGPointMake(startLocX, 0);
    
    //得到末尾位置    x
    CGFloat locX = arc4random_uniform(3 * SCREENSIZE.width * 10)/10.0 - SCREENSIZE.width;
    CGFloat locY = arc4random_uniform(SCREENSIZE.width * 10)/10.0 + SCREENSIZE.height;
    
    
    UIImageView *bulletView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@(arc4random_uniform(6)+1).description]];
    [_bulletArray addObject:bulletView];
    
    bulletView.center = startLoc;
    [self.view addSubview:bulletView];
    [UIView animateWithDuration:2.0 animations:^{
        bulletView.center = CGPointMake(locX, locY);
    }completion:^(BOOL finished) {
        [_bulletArray removeObject:bulletView];
        [bulletView removeFromSuperview];
    }];
    
}

@end
